package cn.bluedot.tea.util;

import java.util.Arrays;
import java.util.List;

public class PageBean<T> {
	//当前页
    private int pageNum;
    //每页的记录数量
    private int size;
  
    //总记录数
    private long total;
    //总页数
    private int pages;
    //结果集
    private List<T> list;
    
    //导航条上的第一页
    private int firstPage;
    //导航条上的最后一页
    private int lastPage;

    //导航条的页码数
    private int navigatePages;
    //所有导航页码
    private int[] navigateNums;
    
        
    public PageBean(List<T> list, int navigatePages) {
    	
    	if(list != null) {
    		Page<T> page = Page.getLocalPage();
	        this.pageNum = page.getPageNum();
	        this.size = page.getPageSize();
	        
	        //若没有设置total，默认为结果集大小
	        this.total = initTotal(page, list.size());
	        this.pages = initPages(page);
	        
	        this.navigatePages = navigatePages;
			
	        calculateNavigate();
			this.list = initList(list);
    	}
       
    	
		Page.clearPage();
    }
    
	private long initTotal(Page<T> page, int size) {
		
		long total = page.getTotal();
		
		if(total == -1 || total == 0) {
			return size;
		}
		
		return page.getTotal();
	}
	
	private int initPages(Page<T> page) {
		int pages = page.getPages();
		
        if (pages == -1 && size > 0) {
            return (int) (total / size + ((total % size == 0) ? 0 : 1));
        }
        
        return 0;
	}

	private List<T> initList(List<T> list) {
		
		//一般limit查询结果大小限制在了size，大于size时需要分割舍去多余结果
		
		if(list.size() == total) { //查询结果为全部结果集
			if(this.pageNum > pages) {
				this.pageNum = pages;
			}else if(this.pageNum <= 0) {
				this.pageNum = 1;
			}
			
			int startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.size : 0;
	        int endRow = startRow + this.size * (this.pageNum > 0 ? 1 : 0);
			
			if(endRow > total) {
				endRow = (int) total;
			}
			
			return list.subList(startRow, endRow);
		}
		
		if(list.size() > size) { //查询结果出现多出size条数时，多出部分舍弃
			return list.subList(0, size);
		}
		
		return list;  //limit查询正常情况下，list.size() = size
	}

	private void calculateNavigate() {
		
		if(pages < navigatePages) {//总页数小于列表长度
			firstPage = 1;
			lastPage = pages;
			navigateNums = new int[pages];
            for (int i = 0; i < pages; i++) {
                navigateNums[i] = i + 1;
            } 
		} else { //当总页数大于导航页码数时
			firstPage = pageNum - navigatePages / 2;
			lastPage = pageNum + navigatePages / 2;      
            navigateNums = new int[navigatePages];
            int temp = firstPage;
            if(firstPage < 1) {//头溢出
            	firstPage = 1;
            	temp = firstPage;
            	lastPage = navigatePages;
            	for (int i = 0; i < navigatePages; i++) {
                    navigateNums[i] = temp++;
                }
            } else if(lastPage > pages) {//尾溢出
            	firstPage = pages - (navigatePages - 1);
            	lastPage = pages;
            	temp = lastPage;
            	for (int i = navigatePages - 1; i >= 0; i--) {
                    navigateNums[i] = temp--;
                }
            } else {
                for (int i = 0; i < navigatePages; i++) {
                    navigateNums[i] = temp++;
                }
            }
            
        }
		
	}



	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		if(this.size == 0) {
			return 0;
		}
		pages = (int) (total / size);
		return pages % size == 0 ? pages : (pages + 1);
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getNavigatePages() {
		return navigatePages;
	}

	public void setNavigatePages(int navigatePages) {
		this.navigatePages = navigatePages;
	}

	public int[] getNavigateNums() {
		return navigateNums;
	}

	public void setNavigateNums(int[] navigateNums) {
		this.navigateNums = navigateNums;
	}

	@Override
	public String toString() {
		return "PageBean [pageNum=" + pageNum + ", size=" + size + ", total=" + total + ", pages=" + pages + ", list="
				+ list + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", navigatePages=" + navigatePages
				+ ", navigateNums=" + Arrays.toString(navigateNums) + "]";
	}
    
}
